/**
 * Created by fupeng on 17/3/12.
 */
/**
 * Created by fupeng on 17/3/9.
 */
var cinemaFootPrintDao =  require('../dao/CinameFootPrintDao');
var memberDao = require('../dao/MemberDao');
var request = require('request');
var apiurl = require('../../config').FPEsever.cinemaapiurl;

module.exports = {
    getCinemaInfoLimitModel:function(req, callback) {
        if (/^\d+$/.test(req.body.lastIndex)) {
            var open_id = req.body.open_id;
            if (open_id) {
                var requresData = {
                    openId:open_id,
                    lastIndex:req.body.lastIndex
                };
                request({
                    headers:{
                        'Content-Type':'application/json'
                    },
                    url:apiurl,
                    method:'post',
                    body: JSON.stringify(requresData)
                },function (err, response, body) {
                    if(err){
                        console.log('足迹获取失败'+err)
                        callback({
                            message: 'error',
                            status: 'error',
                            data: ''
                        });
                        return;
                    }
                    try {
                        cinemaInfo = eval('('+body+')').data;
                        var sqlfoot ='';
                        cinemaInfo.forEach(function(val,i){
                            if(i === 0){
                                sqlfoot += ' select * ,\''+ val._id.createtime+ '\' as create_time from cinema where cinema_code =  \''+ val._id.cid +'\'';
                            }else{
                                sqlfoot += ' UNION all select * ,\''+ val._id.createtime+ '\' as create_time from cinema where cinema_code = \''+ val._id.cid +'\'';
                            }

                        });
                        if(sqlfoot){
                            cinemaFootPrintDao.getCinema({
                                lat: req.body.lat,
                                long: req.body.long,
                                cinematable:sqlfoot
                            }, function (data) {
                                if (data.isSuccess) {
                                    data.rows.forEach(function (val,i) {
                                        cinemaInfo[i] = {
                                            cinema_code: val.cinema_code,
                                            cinemaName: val.cinema_name,
                                            cinemaAddress: val.cinema_site,
                                            create_time:val.create_time,
                                            cinema_picture: val.cinema_picture,
                                            distance: val.distance + '',
                                            score: val.score
                                        };

                                    });
                                    callback({
                                        message: 'success',
                                        status: 'success',
                                        data: cinemaInfo
                                    });
                                } else {
                                    callback({
                                        message: 'error',
                                        status: 'error',
                                        data: ''
                                    });
                                }
                            })
                        }else{
                            callback({
                                message: 'success',
                                status: 'success',
                                data: ''
                            });
                        }
                    } catch (error) {
                        // 解析失败
                        console.log("获取足迹: Error!" + error.stack);
                        callback({
                            message: 'error',
                            status: 'error',
                            data: ''
                        });

                    }
                })
                //     if (data.rows[0]) {
                //         var member_id = data.rows[0].member_id;
                //         cinemaFootPrintDao.getCinema({
                //             citycode: req.body.citycode,
                //             cinema_name: req.body.cinema_name,
                //             lat: req.body.lat,
                //             long: req.body.long,
                //             lastIndex: parseInt(req.body.lastIndex),
                //             member_id: member_id
                //         }, function (data) {
                //             var cinemaInfo = [];
                //             if (data.isSuccess) {
                //                 data.rows.forEach(function (val) {
                //                     cinemaInfo.push({
                //                         cinema_code: val.cinema_code,
                //                         cinemaName: val.cinema_name,
                //                         cinemaAddress: val.cinema_site,
                //                         cinema_picture: val.cinema_picture,
                //                         distance: val.distance + '',
                //                         score: val.score
                //                     });
                //
                //                 });
                //                 callback({
                //                     message: 'success',
                //                     status: 'success',
                //                     data: cinemaInfo
                //                 });
                //             } else {
                //                 callback({
                //                     message: 'error',
                //                     status: 'error',
                //                     data: ''
                //                 });
                //             }
                //         })
                //     }
            }
        }else{
            callback({
                message: 'error',
                status: 'error',
                data: ''
            });
        }
    }
}
